package densetsu.model.map;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import densetsu.model.map.tiles.BombTile;
import densetsu.model.map.tiles.CoinTile;
import densetsu.model.map.tiles.DoorTile;
import densetsu.model.map.tiles.GrassTile;
import densetsu.model.map.tiles.StoneTile;
import densetsu.model.map.tiles.Tile;
import densetsu.model.map.tiles.WallTile;

public class MapLoader {

	public static final String WALL = "w", GRASS = "g", COIN = "c", BOMB = "b", STONE = "s",
			DOOR = "d";

	public static Tile[][] getMap ( String fileName ) throws FileNotFoundException {
		Tile[][] map = new Tile[GameMap.SIZE][GameMap.SIZE];
		File mapFile = new File ( fileName );
		if ( !mapFile.exists ( ) ) {
			throw new FileNotFoundException ( "File in path " + fileName + " could not be found!" );
		} else {
			Scanner scanner = new Scanner ( mapFile );
			int currentY = 0;
			while ( scanner.hasNextLine ( ) && currentY < GameMap.SIZE ) {
				String[] line = scanner.nextLine ( ).split ( " " );
				for ( int x = 0 ; x < GameMap.SIZE ; x++ ) {
					switch ( line[x].trim ( ) ) {
					case WALL:
						map[x][currentY] = new WallTile ( );
						break;
					case GRASS:
						map[x][currentY] = new GrassTile ( );
						break;
					case COIN:
						map[x][currentY] = new CoinTile ( );
						break;
					case BOMB:
						map[x][currentY] = new BombTile ( );
						break;
					case STONE:
						map[x][currentY] = new StoneTile ( );
						break;
					case DOOR:
						map[x][currentY] = new DoorTile ( );
						break;
					default:
						map[x][currentY] = new GrassTile ( );
						break;
					}
				}
				currentY++;
			}
			scanner.close ( );
			return map;
		}
	}

}
